package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.TeacherCondition;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ht
 * @since 2020-04-11
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Resource
    EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        R.ok().data("list",list);
        try {
            int i=10/0;
        } catch (Exception e) {
            throw new GuliException();
        }

        return R.ok().data("list",list);

    }
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R delete(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        boolean b = eduTeacherService.removeById(id);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @ApiOperation(value = "查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable Long current,@PathVariable Long limit){
        Page<EduTeacher> page = new Page<>(current, limit);
        IPage<EduTeacher> page1 = eduTeacherService.page(page, null);
        return R.ok().data("page",page1);
    }

    @ApiOperation(value = "条件分页查询讲师")
    @GetMapping("pageTeacherCondition/{current}/{limit}")
    public R pageListTeacherCondition(@PathVariable Long current, @PathVariable Long limit, TeacherCondition teacherCondition){
        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if(teacherCondition.getName()!=null){
            wrapper.like("name",teacherCondition.getName());
        }
        if(teacherCondition.getLevel()!=null){
            wrapper.eq("level",teacherCondition.getLevel());
        }
        if(teacherCondition.getBgmtCreate()!=null){
            wrapper.gt("gmtCreate",teacherCondition.getBgmtCreate());
        }
        if(teacherCondition.getEgmtCreate()!=null){
            wrapper.lt("gmtCreate",teacherCondition.getEgmtCreate());
        }
        IPage<EduTeacher> page1 = eduTeacherService.page(page, wrapper);
        return R.ok().data("page",page1);
    }

    @ApiOperation(value = "保存讲师")
    @PostMapping("save")
    public R save(@RequestBody(required = false) EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @ApiOperation(value = "修改讲师")
    @PostMapping("update")
    public R update(@RequestBody(required = false) EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        if(update){
            return R.ok();
        }else{
            return R.error();
        }
    }


}

